package com.budwk.app.miniapp.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.app.miniapp.models.WxContainer;
import com.budwk.app.miniapp.services.WxcontainerService;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/miniapp/container")
@ApiDefinition(tag = "微信小程序-雨量杯管理")
@Slf4j
public class WxcontainerController {
    @Inject
    private WxcontainerService wxContainerService;

    @At("/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询雨量杯")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = WxContainer.class
    )
    @ApiResponses(
            implementation = WxContainer.class
    )
    @SaCheckLogin
    public Result<?> list(@Param("..") WxContainer bean,
                          @Param("pageNo") int pageNo,
                          @Param("pageSize") int pageSize,
                          @Param("pageOrderName") String pageOrderName,
                          @Param("pageOrderBy") String pageOrderBy) {
        return Result.data(wxContainerService.listPage(pageNo, pageSize, wxContainerService.querySqlBuild(bean, pageOrderName, pageOrderBy)));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增雨量杯")
    @ApiFormParams(
            implementation = WxContainer.class
    )
    @ApiResponses
    @SLog(tag = "新增雨量杯", value = "${args[0].containerId}")
    @SaCheckLogin
    @Aop(TransAop.READ_COMMITTED)
    public Result<?> create(@Param("..") WxContainer wxContainer, HttpServletRequest req) {
        //wxContainer.setCreatedBy(SecurityUtil.getUserId());
        wxContainerService.insert(wxContainer);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改雨量杯")
    @ApiFormParams(
            implementation = WxContainer.class
    )
    @ApiResponses
    @SLog(tag = "修改雨量杯", value = "${args[0].containerId}")
    @SaCheckLogin
    @Aop(TransAop.READ_COMMITTED)
    public Result<?> update(@Param("..") WxContainer wxContainer, HttpServletRequest req) {
        //wxContainer.setUpdatedBy(SecurityUtil.getUserId());
        wxContainerService.updateIgnoreNull(wxContainer);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取雨量杯")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SaCheckLogin
    public Result<?> getData(@Param("id") String id, HttpServletRequest req) {
        WxContainer wxContainer = wxContainerService.fetch(id);
        if (wxContainer == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxContainer);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除雨量杯")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "删除雨量杯", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        WxContainer wxContainer = wxContainerService.fetch(id);
        if (wxContainer == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxContainerService.delete(id);
        req.setAttribute("_slog_msg", wxContainer.getContainerId());
        return Result.success();
    }

    @At("/switchAll")
    @Ok("json")
    @POST
    @ApiOperation(name = "推送开关")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "推送开关", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> switchFillAllFlag(@Param("..") WxContainer wxContainer, HttpServletRequest req) {
        return Result.success(wxContainerService.switchFillAllFlag(wxContainer));
    }
}
